﻿using DotNetty.Common.Utilities;
using DotNetty.Transport.Channels;
using PSAMMatrixCloud.Common;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSAMMatrixCloud.TransferSecret.PSAMMatrix
{
    /// <summary>
    /// Channel Manager
    /// </summary>
    public static class PSAMChannelManager 
    {
        private static readonly ConcurrentDictionary<string, SessionInfo> dicSession = new ConcurrentDictionary<string, SessionInfo>();

        public static void Add(string deviceID, SessionInfo session) 
        {
            dicSession[deviceID] = session;
        }

        public static SessionInfo GetChannel(string deviceID)
        {
            if (dicSession.TryGetValue(deviceID, out SessionInfo value))
            {
                return value;
            }

            return null;
        }

        /// <summary>
        /// 获得空闲PSAM加密盒子
        /// </summary>
        /// <returns></returns>
        public static string GetIdleMatrixNo()
        {
            var PSAMMatrixValue = dicSession.Values.Max(n => n.PSAMIdleCount);

            return dicSession.Values.FirstOrDefault(p => p.PSAMIdleCount == PSAMMatrixValue).PSAMMatrixNo;
        }
    }
}
